<?php
namespace Common\Common;
class Createcsv{

//路径
private $file_path;

function __construct($file_path){
$this->file_path=$file_path;
}

//将数组变为字符串
public function array_to_string($result) {

$data = '#,商户#,顾客邮箱,订单金额,订单状态,订单时间,网站ID,账单国家,货运公司,物流单号'."\n"; //栏目名称

foreach($result as $key=>$value){
$data .= $value['order_id'].','.
$value['mh_oid'].','.
$value['bill_email'].','.
$value['order_amount']." ".$value['order_currency'].','.
orderStatus($value['order_status']).','.
$value['order_time'].','.
$value['order_siteid'].','.
$value['bill_country'].','.
$value['tracking_url'].','.
$value['tracking_num'].','.
"\n";
}
//dump($result);

//写文件
$file=fopen($this->file_path,"w");
fwrite($file,$this->i($data));
fclose($file);
//dump($this->i($data));
}

private function i($strInput) {
return iconv('utf-8','gb2312',$strInput);//页面编码为utf-8时使用，否则导出的中文为乱码
}

//下载
public function down_csv($name){
            $file = fopen($this->file_path,"r"); 
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: ".filesize($this->file_path));
            Header("Content-Disposition: attachment; filename=".$name);
            echo fread($file, filesize($this->file_path));
            fclose($file);
} 

}
?>